require 'iconv'

class Petrinets < Application
  before :find_petrinet, :exclude => [ :new, :create, :index ]

  def create
    begin
      petrinet = PNMLParser.parse(Iconv.iconv("utf-8", "iso-8859-1", params['pnml_content'])[0])
      petrinet.save!
      return ""
    rescue Exception => ex
      Merb.logger.error(ex)
      Merb.logger.error(ex.backtrace.join("\n"))
      self.status = 500
      return ex.to_s
    end
  end

  def show
    provides :html, :pnml

    #set up template data
    @title = 'Petrinet'
    @title = @title + ' ' + @petrinet.name if @petrinet.name
    
    #temporary fix
    case content_type
    when :html
      render :status => 200
    when :pnml
      @petrinet.to_pnml
    end
  end

  def destroy
    @petrinet.destroy
    return ""
  end

  private
  def find_petrinet
    @petrinet = PNEngine::Petrinet.find(params[:id] || params[:petrinet_id])
    @case = PNEngine::Case.find(params[:case_id]) if params[:case_id]
  end
end